package com.dhkj.web.utils;


import android.content.Context;
import android.util.Base64;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhkj.web.bean.DiningTableListDto;
import com.dhkj.web.bean.JiaoBanRes;
import com.dhkj.web.bean.KlStateRes;
import com.dhkj.web.bean.MacMainpFoodMenu;
import com.dhkj.web.bean.MacMainpSbruanDinner;
import com.dhkj.web.bean.ReserveListDto;
import com.dhkj.web.bean.SysUserModel;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * 提交参数方式
 */
public class RequestUtils {

    public static void login(Context context, String user, String pass, String deviceId, MyObserverRes<String> observer) {

        RetrofitUtils.getApiUrl().login(user, pass, deviceId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void deviceSign(Context context, String deviceId, MyObserverRes<String> observer) {
        RetrofitUtils.getApiUrl().deviceSign(deviceId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getBaseSysInfo(Context context, String userId, String deviceId, MyObserverUZip<JSONObject> observer) {
        RetrofitUtils.getApiUrl().getBaseSysInfo(userId, deviceId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void onLineConsume(Context context, String json, MyObserverRes<JSONObject> consumer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl()
                .onLineConsume(requestBody).compose(RxHelper.observableIO2Main(context))
                .subscribe(consumer);
    }


    public static void checkOrder(Context context, String json, MyObserverRes<JSONObject> consumer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl()
                .checkOrder(requestBody).compose(RxHelper.observableIO2Main(context))
                .subscribe(consumer);
    }

    public static void closeOrder(Context context, String json, MyObserverRes<JSONObject> consumer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl()
                .closeOrder(requestBody).compose(RxHelper.observableIO2Main(context))
                .subscribe(consumer);
    }

    public static void refundProduct(Context context, String json, MyObserverRes<JSONObject> consumer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl()
                .refundProduct(requestBody).compose(RxHelper.observableIO2Main(context))
                .subscribe(consumer);
    }

    public static void onLineConsumeCash(Context context, String json, MyObserverRes<JSONObject> consumer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl()
                .onLineConsumeCash(requestBody).compose(RxHelper.observableIO2Main(context))
                .subscribe(consumer);
    }

    public static void facePay(Context context, String json, MyObserverRes<JSONObject> consumer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitUtils.getApiUrl()
                .facePay(requestBody).compose(RxHelper.observableIO2Main(context))
                .subscribe(consumer);
    }

    public static void getFk(Context context, String pageNum, String pageSize, String deviceId, MyObserverUZip<JSONObject> observer) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceId", deviceId);
        jsonObject.put("pageNum", pageNum);
        jsonObject.put("pageSize", pageSize);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toJSONString());

        RetrofitUtils.getApiUrl().getFk(requestBody).compose(RxHelper.observableIO2Main2(context)).subscribe(observer);
    }

    public static void foodsMenuAdd(Context context, String name, String price, MyObserverRes<String> observer) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
        jsonObject.put("price", price);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toJSONString());
        RetrofitUtils.getApiUrl().foodsMenuAdd(requestBody).compose(RxHelper.observableIO2Main2(context)).subscribe(observer);
    }

    public static void getFoodsMenuList(Context context, MyObserverRes<List<MacMainpFoodMenu>> observer) {
        RetrofitUtils.getApiUrl().getFoodsMenuList().compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getServerTime(Context context, MyObserverRes<String> observer) {
        RetrofitUtils.getApiUrl().getServerTime().compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void addSbruanDinner(Context context, String deviceId,
                                       String bm, String beginTime, String endTime, MyObserverRes<List<MacMainpSbruanDinner>> observer) {
        JSONObject jsonObject = new JSONObject();
        RetrofitUtils.getApiUrl().addSbruanDinner(deviceId, bm, beginTime, endTime).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getJiaoBan(Context context, String deviceId,
                                  String startTime, String endTime, List<String> posNumList, MyObserverRes<JiaoBanRes> observer) {
        JSONObject jsonObject = new JSONObject();
        RetrofitUtils.getApiUrl().getJiaoBan(deviceId, startTime, endTime, posNumList).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    public static void unOnLine(Context context, JSONObject jsonObject, MyObserverRes<String> observer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toJSONString());
        RetrofitUtils.getApiUrl().unOnLine(requestBody).compose(RxHelper.observableIO2Main2(context)).subscribe(observer);
    }


    public static void getKlState(Context context, String deviceId, MyObserverRes<List<KlStateRes>> observer) {
        RetrofitUtils.getApiUrl().getKlState(deviceId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getFk(Context context, String deviceId, String cardsn, MyObserverRes<JSONObject> observer) {
        RetrofitUtils.getApiUrl().getFk(deviceId, cardsn).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getLcReport(Context context, String deviceId, String auth, MyObserverRes<JSONObject> observer) {
        RetrofitUtils.getApiUrl().getLcReport(deviceId, auth).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getTakeFoodList(Context context, String deviceId, String auth, MyObserverRes<JSONObject> observer) {
        RetrofitUtils.getApiUrl().getTakeFoodList(deviceId, auth).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void receiveFood(Context context, List<Long> pkIdList, MyObserverRes<String> observer) {
        RetrofitUtils.getApiUrl().receiveFood(pkIdList).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getOrderList(Context context, String deviceId, String startTime, String endTime, String posnum, MyObserverRes<List<ReserveListDto>> observer) {
        RetrofitUtils.getApiUrl().getOrderList(deviceId, startTime, endTime, posnum).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getDiningTableList(Context context, Integer type, String startTime, String endTime, String bqh, String ksh, MyObserverRes<List<DiningTableListDto>> observer) {
        RetrofitUtils.getApiUrl().getDiningTableList(type, startTime, endTime, bqh, ksh).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void cleanDiningTable(Context context, String ksh, String dinner, String dcTime, MyObserverRes<String> observer) {
        RetrofitUtils.getApiUrl().cleanDiningTable(ksh, dinner, dcTime).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getDiningTableDetails(Context context, String ksh, String dinner, String dcTime, String times, MyObserverRes<JSONArray> observer) {
        RetrofitUtils.getApiUrl().getDiningTableDetails(ksh, dinner, dcTime, times).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getOrderDetails(Context context, String orders, MyObserverRes<JSONArray> observer) {
        RetrofitUtils.getApiUrl().getOrderDetails(orders).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    public static void deleteKl(Context context, String id, String deviceId, MyObserverRes<List<KlStateRes>> observer) {
        RetrofitUtils.getApiUrl().deleteKl(id, deviceId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    public static void base64Upload(Context context, byte[] base64b, MyObserverRes<String> observer) {

        RetrofitUtils.getApiUrl().base64Upload("data:image/jpg;base64," + Base64.encodeToString(base64b, Base64.DEFAULT))
                .compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getInfo(Context context, String deviceId, int table, MyObserverUZip<JSONObject> observer) {

        RetrofitUtils.getApiUrl().getInfo(deviceId, table).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getWeekMenuBak(Context context, String deviceId, int table, String savedm, String date, MyObserverUZip<JSONObject> observer) {

        RetrofitUtils.getApiUrl().getWeekMenuBak(deviceId, table, savedm, date).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void saveWeekMenuBak(Context context, JSONObject jsonObject, MyObserverRes<String> observer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toJSONString());
        RetrofitUtils.getApiUrl().saveWeekMenuBak(requestBody).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getPosNumList(Context context, String addressNo, MyObserverRes<List<SysUserModel>> observer) {
        RetrofitUtils.getApiUrl().getPosNumList(addressNo).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    /**
     * Post 请求demo
     */
//    public static void postDemo(RxAppCompatActivity context, String name, String password, Observer<Demo> consumer){
//        RetrofitUtils.getApiUrl()
//                .postUser(name,password).compose(RxHelper.observableIO2Main(context))
//                .subscribe(consumer);
//    }
    /**
     * Put 请求demo
     */
//    public static void putDemo(RxFragment context, String access_token,Observer<Demo> consumer){
//        Map<String, String> headers = new HashMap<String, String>();
//        headers.put("Accept","application/json");
//        headers.put("Authorization",access_token);
//        RetrofitUtils.getApiUrl()
//                .put(headers,"厦门").compose(RxHelper.observableIO2Main(context))
//                .subscribe(consumer);
//    }
    /**
     * Delete 请求demo
     */
//    public static void deleteDemo(RxFragment context, String access_token,Observer<Demo> consumer){
//        RetrofitUtils.getApiUrl()
//                .delete(access_token,1).compose(RxHelper.observableIO2Main(context))
//                .subscribe(consumer);
//    }

    /**
     * 上传图片
     */
//    public static void upImagView(RxFragment context, String  access_token,String str, Observer<Demo>  observer){
//        File file = new File(str);
////        File file = new File(imgPath);
//        Map<String,String> header = new HashMap<String, String>();
//        header.put("Accept","application/json");
//        header.put("Authorization",access_token);
////        File file =new File(filePath);
//        RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
////        RequestBody requestFile =
////                RequestBody.create(MediaType.parse("multipart/form-data"), file);
//        MultipartBody.Part body =
//                MultipartBody.Part.createFormData("file", file.getName(), reqFile);
//        RetrofitUtils.getApiUrl().uploadImage(header,body).compose(RxHelper.observableIO2Main(context))
//                .subscribe(observer);
//    }

    /**
     * 上传多张图片
     */
//    public static void upLoadImg(RxFragment context,String access_token,List<File> files, Observer<Demo>  observer1){
//        Map<String,String> header = new HashMap<String, String>();
//        header.put("Accept","application/json");
//        header.put("Authorization",access_token);
//        MultipartBody.Builder builder = new MultipartBody.Builder()
//                .setType(MultipartBody.FORM);//表单类型
//        for (int i = 0; i < files.size(); i++) {
//            File file = files.get(i);
//            RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/*"), file);
//            builder.addFormDataPart("file", file.getName(), photoRequestBody);
//        }
//        List<MultipartBody.Part> parts = builder.build().parts();
//        RetrofitUtils.getApiUrl().uploadImage1(header,parts).compose(RxHelper.observableIO2Main(context))
//                .subscribe(observer1);
//    }
}

